package com.learn.springboot3.controller;

import cn.hutool.crypto.digest.MD5;
import com.learn.springboot3.config.MinioPropertiesConfig;
import com.learn.springboot3.controller.dto.FileUploadDto;
import io.minio.*;
import io.minio.errors.*;
import io.minio.messages.Item;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.concurrent.TimeUnit;

/**
 * ClassName: UploadController
 * Package: com.learn.springboot3.controller
 * Description:
 *
 * @Author jiangcheng
 * @Create 2024/9/27 14:49
 * @Version 1.0
 */
@RestController
@RequestMapping("upload")

@CrossOrigin
public class UploadController {
    private static final Logger log = LoggerFactory.getLogger(UploadController.class);
    @Autowired
    private MinioClient minioClient;
    @Autowired
    private MinioPropertiesConfig minioPropertiesConfig;

    @Autowired
    private StringRedisTemplate redisTemplate;


    @PostMapping("/test")

    public String test(MultipartFile file,
                      String chunkIndex,
                       String totalChunks,
                       String chunkFileMd5,
                       String fileMd5) throws ServerException, InsufficientDataException, ErrorResponseException, IOException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException, ServerException, InsufficientDataException, ErrorResponseException, InvalidKeyException {
        System.out.println("file = " + file);

        System.out.println("chunkIndex = " + chunkIndex);
        System.out.println("totalChunks = " + totalChunks);

        System.out.println("chunkFileMd5 = " + chunkFileMd5);
        System.out.println("fileMd5 = " + fileMd5);
        String s = MD5.create().digestHex(file.getBytes());
        log.error(s);

      String key =  fileMd5+"/"+s+"@"+chunkIndex;
        minioClient.listObjects(ListObjectsArgs.builder()
                        .bucket(minioPropertiesConfig.getBucketName())
                        .prefix(fileMd5)
                        .recursive(true)
                        .build())
                .forEach(object -> {
                    try {
                        System.out.println(object.get().objectName());
                    } catch (ErrorResponseException e) {
                        throw new RuntimeException(e);
                    } catch (InsufficientDataException e) {
                        throw new RuntimeException(e);
                    } catch (InternalException e) {
                        throw new RuntimeException(e);
                    } catch (InvalidKeyException e) {
                        throw new RuntimeException(e);
                    } catch (InvalidResponseException e) {
                        throw new RuntimeException(e);
                    } catch (IOException e) {
                        throw new RuntimeException(e);
                    } catch (NoSuchAlgorithmException e) {
                        throw new RuntimeException(e);
                    } catch (ServerException e) {
                        throw new RuntimeException(e);
                    } catch (XmlParserException e) {
                        throw new RuntimeException(e);
                    }
                });
        return "ok";
    }


    public  String checkFileMd5(String chunkFileMd5,File file, Integer currentChunkSize){

        String s = MD5.create().digestHex(file);

        String keys = chunkFileMd5 +"@"+"@"+ currentChunkSize;

        redisTemplate.opsForValue().set("", s,240, TimeUnit.HOURS);

        return s;
    }
}
